package com.itic.appbase.applications.sys.org.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itic.appbase.applications.sys.org.persistence.dao.OrgDao;
import com.itic.appbase.applications.sys.org.persistence.model.Org;
import com.itic.appbase.applications.sys.org.persistence.model.OrgExample;
import com.itic.appbase.applications.sys.org.persistence.model.OrgExample.Criteria;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.appbase.framework.utils.PropertyGetter;
import com.itic.appbase.framework.utils.StringHelper;

@Service
@Transactional
public class OrgService
        extends CrudService<OrgDao, Org, OrgExample> {
    @Autowired
    private OrgDao orgDao;
	private OrgExample example;

    /**
     * 根据code/pcode/clazz查询list
     * 
     * @param queryOrg
     * @return
     */
    public List<Org> selectOrgList(Org queryOrg) {
    	String isMysql = PropertyGetter.getString("app.db.type");
    	if( StringHelper.isNotBlank(isMysql)&&"mysql".equals(isMysql) ){
    		 example = new OrgExample();
    		 Criteria criteria = example.createCriteria();
    		 criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    		 if(StringHelper.isNotBlank(queryOrg.getpCode())){
    			 //criteria.andCodeEqualTo(queryOrg.getpCode());
    			 criteria.andPCodeEqualTo(queryOrg.getpCode());
    		 }
    		 List<Org> list = orgDao.selectByExample(example);
    		 if( list.size() > 0 ){
    			 example.clear();
    			 criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    			 ArrayList<String> idList = new ArrayList<String>();
    			 for( Org temp : list ){
    				 idList.add(temp.getCode());
    			 }
    			 criteria.andPCodeIn(idList);
    			 return orgDao.selectByExample(example);
    			 //list.addAll(list2);
    		 }else{
    			 return list;
    		 }
    		 //return list;
    		 //return orgDao.selectOrgTree(queryOrg);
    	}else{
    		 return orgDao.selectOrgTree(queryOrg);
    	}
    }

    /**
     * 根据code查询条件获取org
     * 
     * @param code
     * @return
     */
    public Org selectOrgByCode(String code) {
        Org org = null;
        if (StringHelper.isNotBlank(code)) {
            Org orgTemp = new Org();
            orgTemp.setCode(code);
            orgTemp.setIsEffective(DBConst.TRUE);
            org = orgDao.selectOrgByCode(orgTemp);
        }
        return org;

    }

    /**
     * 根据id级联删除
     * 
     * @param id
     * @return
     */
    public String deleteOrgTree(String id) {
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("id", id); // id
        paramMap.put("dbTrue", DBConst.TRUE); // dbTrue
        paramMap.put("dbFalse", DBConst.FALSE); // dbFalse
        String isMysql = PropertyGetter.getString("app.db.type");
        int code = 0;
    	if( StringHelper.isNotBlank(isMysql)&&"mysql".equals(isMysql) ){
    		//code = orgDao.deleteOrgTree(paramMap);
    		Org org = orgDao.selectByPrimaryKey(id);
    		 example = new OrgExample();
    		 example.createCriteria().andPCodeEqualTo(org.getCode());
    		 code += orgDao.deleteByPrimaryKey(id);
    		 code += orgDao.deleteByExample(example);
    		 //return orgDao.selectByExample(example);
    	}else{
    		code = orgDao.deleteOrgTree(paramMap);
    	}
       
        return code > 0 ? SUCCESS : FAILED;
    }

}